package MyGO.controller;

import MyGO.Data.GlobalData;
import MyGO.Main;
import MyGO.controller.MyController.PushWindow;
import MyGO.property.Medicine;
import MyGO.service.IMedicineService;
import MyGO.service.impl.MedicineServiceImpl;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.*;

import javafx.util.Callback;
import javafx.util.converter.NumberStringConverter;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.util.List;

public class PrescriMedicationController {
    @FXML
    private Label PID;
    @FXML
    private Label PIDW;
    @FXML
    private Label Page;
    @FXML
    private Label PageW;
    @FXML
    private Label Pgender;
    @FXML
    private Label PgenderW;
    @FXML
    private Label Pname;
    @FXML
    private Label PnameW;
    @FXML
    private Label label1;
    @FXML
    private Label label_way;
    @FXML
    private Button b_reset;
    @FXML
    private Button b_setup;
    @FXML
    private Button choose_medicine;
    @FXML
    private TableColumn<Medicine, String> chooseCloumn;
    @FXML
    private TableColumn<Medicine, String> medicineFormatCloumn;
    @FXML
    private TableColumn<Medicine, String> medicineNameCloumn;
    @FXML
    private TableColumn<Medicine, Void> medicineNumCloumn;
    @FXML
    private TableColumn<Medicine, Double> medicinePriceCloumn;
    @FXML
    private TableColumn<Medicine, Void> medicineUsedCloumn;

    @FXML
    @Setter
    @Getter
    private TextField priceAll;

    private DoubleProperty fee = new SimpleDoubleProperty(0.0);

    @FXML
    @Getter
    private TableView<Medicine> tv_medicine;
    private IMedicineService medicineService = new MedicineServiceImpl();
    @FXML
    public void initialize() {
        List<Medicine> medicines = medicineService.getMedicineList();

//        Bindings.bindBidirectional(priceAll.textProperty(), fee, new NumberStringConverter());
        priceAll.textProperty().bindBidirectional(fee, new NumberStringConverter());


        medicineNameCloumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        medicineFormatCloumn.setCellValueFactory(new PropertyValueFactory<>("format"));
        medicinePriceCloumn.setCellValueFactory(new PropertyValueFactory<>("price"));


        medicineNumCloumn.setCellFactory(new Callback<TableColumn<Medicine, Void>, TableCell<Medicine, Void>>() {

            @Override
            public TableCell<Medicine, Void> call(final TableColumn<Medicine, Void> param) {
                final TableCell<Medicine, Void> cell = new TableCell<Medicine, Void>() {
                    private TextField count = new TextField();
                    private int p_num = 0;
                    {
                        count.setPromptText("请输入数量");
                        count.setOnAction(e -> {
                              Medicine medicine = getTableView().getItems().get(getIndex());
                              int num = Integer.parseInt(count.getText());
                              fee.set(fee.get() + ((num - p_num) * medicine.getPrice()));
                              GlobalData.trackForm.getMedicineForm().countMedicine(medicine, num);
                              System.out.println(num);
                              if (num <= 0) {
                                  tv_medicine.getItems().remove(medicine);
                              }
                              p_num = num;
                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(count);
                        }
                    }
                };
                return cell;
            }
        });

        medicineUsedCloumn.setCellFactory(new Callback<TableColumn<Medicine, Void>, TableCell<Medicine, Void>>() {

            @Override
            public TableCell<Medicine, Void> call(final TableColumn<Medicine, Void> param) {
                final TableCell<Medicine, Void> cell = new TableCell<Medicine, Void>() {
                    private TextField used = new TextField();
                    {
                       used.setPromptText("请输入用法用量提示");
                       used.setOnAction(e -> {
                           Medicine medicine = getTableView().getItems().get(getIndex());
                           GlobalData.trackForm.getMedicineForm().writeMadicineUsed(medicine, used.getText());
                       });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(used);
                        }
                    }
                };
                return cell;
            }
        });
    }


    private void updateFee(double fee) {

    }

    @FXML
    void Reset(ActionEvent event) {
        if (GlobalData.trackForm != null) {
            GlobalData.trackForm.getMedicineForm().getMedicines().clear();
            GlobalData.trackForm.getMedicineForm().getMedicineNum().clear();
            GlobalData.trackForm.getMedicineForm().getMedicineUsed().clear();
            GlobalData.trackForm.getMedicineForm().setFee(0.0);
        }
        priceAll.clear();
        priceAll.setText("0.0");
        tv_medicine.getItems().clear();
    }
    @FXML
    void Setup(ActionEvent event) {
        if (GlobalData.trackForm != null) {
            GlobalData.trackForm.setStatus_vail("未付费");
        }
        tv_medicine.getItems().clear();
    }
    @FXML
    void Choose_medicine(ActionEvent event) throws IOException {
        PushWindow.pushWindow("/MyGO/fxml/add_medicine.fxml", "药品选择", 800, 600,
                AddMedicineControllor.class, ((controller, stage) -> {
                    controller.setMyStage(stage);
                    controller.setPrescriMedicationController(this);
                }));

    }
}



